﻿var xhrGet;                 // for retrieving customer profile 
var xhrPost;                // for updating customer profile
var xhrValidate;            // for immediate validation
var xhrPassword;            // for changing password
var xhrCreditCard;
var xhrPasswordStrength;    // for password strength feedback

var NULL_FST_NAME = 100;     // First name required
var NULL_LST_NAME = 101;     // Last name required
var NULL_EMAIL = 110;        // Email required
var EMAIL_NT_UNIQUE = 111;   // Email already registered
var INVALID_EMAIL = 112;     // Invalid email format
var EMAIL_NT_REG = 113;      // Email not registered
var ACC_ACTIVATED = 114;     // Email already activated
var INVALID_CONTACT = 120;   // Invalid contact format
var INVALID_DOB = 131;       // Invalid DOB
var NULL_DOB = 132;          // Null DOB
var INVALID_CC_EXPIRY = 140; // Invalid CC Expiry date
var CC_EXPIRED = 141;        // CC expired
var INVALID_CC_NUM = 142;    // Invalid CC number
var INVALID_CC_ISSUER = 143; // Wrong issuer
var INVALID_PWD = 150;       // Invalid password
var NULL_PWD = 151;          // Password required
var PWD_MISMATCH = 152;      // Password mismatch
var SYS_ERR = 200;           // Generic system error
/*****************************************************************************/

/* xhrGet */
function getCustomer() {

    /* create xhrGet */
    if (window.ActiveXObject) {
        xhrGet = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xhrGet = new XMLHttpRequest;
    }

    /* check */
    var strCustomerID = getCookie("CustomerID");
    var bCanGet = (strCustomerID != "");

    setControlsEnabled(document.getElementById("fsetRegistrationDetails"), bCanGet);
    setControlsEnabled(document.getElementById("fsetCreditCardDetails"), bCanGet);

    if (!bCanGet) return;
    
    /* build query string */
    var strQuery = 'action=get&id=' + strCustomerID;

    /* set up xhrGet */
    xhrGet.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrGet.onreadystatechange = parseCustomer;
    xhrGet.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrGet.send(strQuery);
}
/*****************************************************************************/
function parseCustomer() {

    if (xhrGet.readyState != 4) return;
    if (xhrGet.status != 200) return;

    /* clear all error message */
    clearAllErrors();
    
    var xhresponse = xhrGet.responseXML;
    
    var nodErrors = xhresponse.getElementsByTagName("Error");
    if (nodErrors.length > 0) {
        /* some error occured, see what is it */
        for (var i = 0; i < nodErrors.length; i++)
            setError(nodErrors[0].getElementsByTagName("ErrorCode")[0].firstChild.nodeValue,
                     nodErrors[0].getElementsByTagName("ErrorMessage")[0].firstChild.nodeValue);
        return;
    }
    
    /* no errors...proceed to populate form */
    var strAttribute;
    var ipAttribute;

    populate(xhresponse, "CustomerID", "txtCustomerID");
    populate(xhresponse, "Password", "txtPassword"); // receive dummy password
    populate(xhresponse, "RegisteredOn", "txtRegisteredOn");

    populate(xhresponse, "FirstName", "txtFirstName");
    populate(xhresponse, "LastName", "txtLastName");
    populate(xhresponse, "Contact", "txtContact");
    populate(xhresponse, "Email", "txtEmail");
    populate(xhresponse, "DOB", "txtDOB");

    populate(xhresponse, "CreditCardNumber", "txtCreditCardNumber");
    populate(xhresponse, "CreditCardCVV", "txtCreditCardCVV");
    populate(xhresponse, "CreditCardType", "selCreditCardType");
    populate(xhresponse, "CreditCardMonth", "selCreditCardMonth");
    populate(xhresponse, "CreditCardYear", "selCreditCardYear");
    populate(xhresponse, "CreditCardHolder", "txtCreditCardHolder");

    populate(xhresponse, "Country", "selCountry");
    populate(xhresponse, "City", "txtCity");
    populate(xhresponse, "State", "txtState");
    populate(xhresponse, "AddressLine1", "txtAddressLine1");
    populate(xhresponse, "AddressLine2", "txtAddressLine2");
    populate(xhresponse, "PostalCode", "txtPostalCode");

    populatePartialText(xhresponse, "CreditCardNumber", "txtCreditCardNumber");

    /* display 'Change credit card' or 'Add credit card'? */
    if(document.getElementById("txtCreditCardNumber").value.length > 0)
        document.getElementById("aChangeCreditCard").innerHTML = "Change credit card";
    else
        document.getElementById("aChangeCreditCard").innerHTML = "Add a credit card";
        
    /* display credit card details fieldset by default: this is for reset button */
    document.forms[0].radSaveCreditCard[0].checked = true;
    showCreditCardFieldset();
}
/*****************************************************************************/

/* xhrPost */
function postCustomer() {
    
    /* create xhrPost */
    if (window.ActiveXObject) {
        xhrPost = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xhrPost = new XMLHttpRequest;
    }

    /* build query string */
    var strQuery = buildPostQuery();

    /* set up xhrPost */
    xhrPost.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrPost.onreadystatechange = handlePostResponse;
    xhrPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrPost.send(strQuery);
}
/*****************************************************************************/
function handlePostResponse() {

    if (xhrPost.readyState != 4) return;
    if (xhrPost.status != 200) return;

    /* remove all error messages */
    clearAllErrors();
    
    /* handle the response */
    var xhresponse = xhrPost.responseXML;
    var nodErrors = xhresponse.getElementsByTagName("Error");

    if (nodErrors.length == 0) {

        /* update welcome message */
        document.getElementById("divWelcome").innerHTML = "Hello! " + getCookie("FirstName");
        
        displayPopup("Your profile has been updated successfully", "center", 2500);
        return;
    }  
    /* handle the errors */
    var nodError;
    var iErrorCode;
    var strErrorMessage;
    
    for (var i = 0; i < nodErrors.length; i++) {
        nodError = nodErrors[i];

        iErrorCode = parseInt(nodError.getElementsByTagName("ErrorCode")[0].firstChild.nodeValue);
        strErrorMessage = nodError.getElementsByTagName("ErrorMessage")[0].firstChild.nodeValue;
        
        setError(iErrorCode, strErrorMessage)
    }
    /* display friendly error message */
    displayPopup("There are invalid fields. Kindly resolve the marked areas", "center", 2500);
}
/*****************************************************************************/
function buildPostQuery() {
    
    var strQuery;
    
    strQuery = "action=post&";
    
    strQuery += "cust_id=" + encodeURIComponent(document.getElementById("txtCustomerID").value) + "&";
    strQuery += "fst_name=" + encodeURIComponent(document.getElementById("txtFirstName").value) + "&";
    strQuery += "lst_name=" + encodeURIComponent(document.getElementById("txtLastName").value) + "&";
    strQuery += "dob=" + encodeURIComponent(document.getElementById("txtDOB").value) + "&";
    strQuery += "contact=" + encodeURIComponent(document.getElementById("txtContact").value) + "&";
    strQuery += "email=" + encodeURIComponent(document.getElementById("txtEmail").value) + "&";
    strQuery += "cc_cvv=" + encodeURIComponent(document.getElementById("txtCreditCardCVV").value) + "&";
    strQuery += "cc_yr=" + encodeURIComponent(document.getElementById("selCreditCardYear").value) + "&";
    strQuery += "cc_mth=" + encodeURIComponent(document.getElementById("selCreditCardMonth").value) + "&";
    strQuery += "cc_hldr=" + encodeURIComponent(document.getElementById("txtCreditCardHolder").value) + "&";
    
    strQuery += "country=" + encodeURIComponent(document.getElementById("selCountry").value) + "&";
    strQuery += "city=" + encodeURIComponent(document.getElementById("txtCity").value) + "&";
    strQuery += "state=" + encodeURIComponent(document.getElementById("txtState").value) + "&";
    strQuery += "addr_l1=" + encodeURIComponent(document.getElementById("txtAddressLine1").value) + "&";
    strQuery += "addr_l2=" + encodeURIComponent(document.getElementById("txtAddressLine2").value) + "&";
    strQuery += "postal=" + encodeURIComponent(document.getElementById("txtPostalCode").value) + "&";

    if (document.forms[0].radSaveCreditCard[0].checked)
        strQuery += "save_cc=yes";
    else
        strQuery += "save_cc=no";
        
    return strQuery;
}
/*****************************************************************************/

/* xhrValidate */
var vfield; // remember which fields is begin validated

function validate(field) {
   
    var strQuery;

    if (field == document.getElementById("txtNewCreditCardNumber"))
        strQuery = "action=v_cc_num&cc_num=" + field.value;

    else if (field == document.getElementById("txtContact"))
        strQuery = "action=v_contact&contact=" + field.value;

    else if (field == document.getElementById("txtFirstName"))
        strQuery = "action=v_fst_name&fst_name=" + field.value;

    else if (field == document.getElementById("txtLastName"))
        strQuery = "action=v_lst_name&lst_name=" + field.value;

    else if (field == document.getElementById("selNewCreditCardType"))
        strQuery = "action=v_cc_issuer&cc_type=" + field.value + "&cc_num=" + document.getElementById("txtNewCreditCardNumber").value;

    else if (field == document.getElementById("selCreditCardMonth") || field == document.getElementById("selCreditCardYear"))
        strQuery = "action=v_cc_expiry&cc_mth=" + document.getElementById("selCreditCardMonth").value + 
                   "&cc_yr=" + document.getElementById("selCreditCardYear").value;

    else if (field == document.getElementById("txtRetypePassword"))
        strQuery = "action=v_retype_pw&new_pw=" + document.getElementById("txtNewPassword").value +
                   "&retype_pw=" + field.value;
                   
    else {
        alert("no field to validate"); // for debugging
        return;
    }
    vfield = field;
    
    /* Create xhrValidate */
    if (window.ActiveXObject) {
        xhrValidate = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xhrValidate = new XMLHttpRequest;
    }

    /* set up xhrValidate */
    xhrValidate.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrValidate.onreadystatechange = handleValidateResponse;
    xhrValidate.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrValidate.send(strQuery);
}
/*****************************************************************************/
function handleValidateResponse() {

    if (xhrValidate.readyState != 4) return;
    if (xhrValidate.status != 200) return;

    var xhresponse = xhrValidate.responseXML;
    var nodErrors = xhresponse.getElementsByTagName("Error");

    if (nodErrors.length == 0) {
        /* remove respective error messages */
        if (vfield == document.getElementById("txtEmail"))
            hideDiv(document.getElementById("divEmailError"));

        else if (vfield == document.getElementById("txtNewCreditCardNumber"))
            hideDiv(document.getElementById("divNewCreditCardNumberError"));

        else if (vfield == document.getElementById("selCreditCardMonth") || vfield == document.getElementById("selCreditCardYear"))
            hideDiv(document.getElementById("divCreditCardExpiryError"));

        else if (vfield == document.getElementById("txtFirstName"))
            hideDiv(document.getElementById("divFirstNameError"));

        else if (vfield == document.getElementById("txtLastName"))
            hideDiv(document.getElementById("divLastNameError"));

        else if (vfield == document.getElementById("txtContact"))
            hideDiv(document.getElementById("divContactError"));

        else if (vfield == document.getElementById("selNewCreditCardType"))
            hideDiv(document.getElementById("divNewCreditCardTypeError"));

        else if (vfield == document.getElementById("txtRetypePassword"))
            hideDiv(document.getElementById("divRetypePasswordError"));
            
        else return;

        return;
    }
    //assert(nodErrors.length == 1);
    var iErrorCode = nodErrors[0].getElementsByTagName("ErrorCode")[0].firstChild.nodeValue;
    var strErrorMessage = nodErrors[0].getElementsByTagName("ErrorMessage")[0].firstChild.nodeValue;
    setError(parseInt(iErrorCode), strErrorMessage);      
}
/*****************************************************************************/

/* xhrPassword */
function sendChangePassword() {

    /* create xhrPassword */
    if (window.ActiveXObject) {
        xhrPassword = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xhrPassword = new XMLHttpRequest;
    }

    /* build query string */
    var strQuery = "action=change_pw&new_pw=" + document.getElementById("txtNewPassword").value +
                    "&retype_pw=" + document.getElementById("txtRetypePassword").value +
                    "&cust_id=" + getCookie("CustomerID");

    /* set up xhrPassword */
    xhrPassword.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrPassword.onreadystatechange = handleChangePasswordResponse;
    xhrPassword.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrPassword.send(strQuery);
}
/*****************************************************************************/
function handleChangePasswordResponse() {

    if (xhrPassword.readyState != 4) return;
    if (xhrPassword.status != 200) return;

    /* remove all error messages */
    hideDiv(document.getElementById("divNewPasswordError"));
    hideDiv(document.getElementById("divRetypePasswordError"));
    hideDiv(document.getElementById("divPasswordStrength"));

    /* handle the response */
    var xhresponse = xhrPassword.responseXML;
    var nodErrors = xhresponse.getElementsByTagName("Error");

    if (nodErrors.length == 0) {
        displayPopup("Your password has been changed successfully", "center", 2500);
        closeChangePassword();
        return;
    }

    /* handle the errors */
    var nodError;
    var iErrorCode;
    var strErrorMessage;

    for (var i = 0; i < nodErrors.length; i++) {
        nodError = nodErrors[i];

        iErrorCode = parseInt(nodError.getElementsByTagName("ErrorCode")[0].firstChild.nodeValue);
        strErrorMessage = nodError.getElementsByTagName("ErrorMessage")[0].firstChild.nodeValue;

        if (iErrorCode == SYS_ERR) 
            showDiv(document.getElementById("divPasswordSystemError"), strError);
        else
            setError(iErrorCode, strErrorMessage)
    }
    /* display friendly error message */
    displayPopup("There are invalid fields. Kindly resolve the marked areas", "center", 2500);
}
/*****************************************************************************/
/* pop up password div */
function showChangePassword() {

    showPopupFieldset(document.getElementById("fsetPassword"));

    /* clears textboxes and errors */
    hideDiv(document.getElementById("divNewPasswordError"));
    hideDiv(document.getElementById("divPasswordStrength"));
    hideDiv(document.getElementById("divRetypePasswordError"));

    document.getElementById("txtNewPassword").value = "";
    document.getElementById("txtRetypePassword").value = "";
}
/*****************************************************************************/
function closeChangePassword() {
    hidePopFieldset(document.getElementById("fsetPassword"));
}
/*****************************************************************************/

/* xhrPasswordStrength */
function getPasswordStrength(field) {

    /* Create xhrPasswordStrength */
    if (window.ActiveXObject) {
        xhrPasswordStrength = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xhrPasswordStrength = new XMLHttpRequest;
    }
    /* build query string */
    var strQuery = 'action=pw_strength&pw=' + field.value;
    
    /* set up xhrValidate */
    xhrPasswordStrength.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrPasswordStrength.onreadystatechange = handlePasswordStrength;
    xhrPasswordStrength.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrPasswordStrength.send(strQuery);
}
/*****************************************************************************/
function handlePasswordStrength() {

    if (xhrPasswordStrength.readyState != 4) return;
    if (xhrPasswordStrength.status != 200) return;
    
    var xhresponse = xhrPasswordStrength.responseXML;
    
    var nodStrength = xhresponse.getElementsByTagName("Strength");

    if (nodStrength.length > 0)
        displayStrength(parseInt(nodStrength[0].firstChild.nodeValue));
    else
        hideStrength();
}
/*****************************************************************************/
function displayStrength(strength) {

    var divPasswordStrength = document.getElementById("divPasswordStrength");
    var divNewPasswordError = document.getElementById("divNewPasswordError");

    switch (strength) {
        case -2:
            showDiv(divNewPasswordError, "Too short");
            hideDiv(divPasswordStrength);
            break;
        case -1:
            showDiv(divNewPasswordError, "Too long");
            hideDiv(divPasswordStrength);
            break;
        case 0:
            showDiv(divNewPasswordError, "Password required");
            hideDiv(divPasswordStrength);
            break;
        case 1:
            showDiv(divPasswordStrength, "<img src='images/weak.png' class='imgPasswordStrength'/>");
            hideDiv(divNewPasswordError);
            break;
        case 2:
            showDiv(divPasswordStrength, "<img src='images/moderate.png' class='imgPasswordStrength'/>");
            hideDiv(divNewPasswordError);
            break;
        case 3:
            showDiv(divPasswordStrength, "<img src='images/strong.png' class='imgPasswordStrength'/>");
            hideDiv(divNewPasswordError);
            break;
        case 4:
            showDiv(divPasswordStrength, "<img src='images/very_strong.png' class='imgPasswordStrength'/>");
            hideDiv(divNewPasswordError);
            break;
        default: hideStrength();
    }
}
/*****************************************************************************/
function hideStrength() {
    
    var divPasswordStrength = document.getElementById("divPasswordStrength");
    divPasswordStrength.style.display = "none";
    divPasswordStrength.innerHTML = "";
}
/*****************************************************************************/

/* Change credit card */
function sendChangeCreditCard() {
    
    /* create xhrCreditCard */
    if (window.ActiveXObject) {
        xhrCreditCard = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
    xhrCreditCard = new XMLHttpRequest;
    }

    /* build query string */
    var strQuery = "action=change_cc&cc_num=" + document.getElementById("txtNewCreditCardNumber").value +
                    "&cc_type=" + document.getElementById("selNewCreditCardType").value +
                    "&cust_id=" + getCookie("CustomerID");

    /* set up xhrCreditCard */
    xhrCreditCard.open("POST", "AccountServlet?timeStamp=" + new Date().getDate(), true);
    xhrCreditCard.onreadystatechange = handleChangeCreditCardResponse;
    xhrCreditCard.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhrCreditCard.send(strQuery);
}
/*****************************************************************************/
function handleChangeCreditCardResponse() {

    if (xhrCreditCard.readyState != 4) return;
    if (xhrCreditCard.status != 200) return;

    /* remove all error messages */
    hideDiv(document.getElementById("divNewCreditCardNumberError"));
    hideDiv(document.getElementById("divNewCreditCardTypeError"));

    /* handle the response */
    var xhresponse = xhrCreditCard.responseXML;
    var nodErrors = xhresponse.getElementsByTagName("Error");

    if (nodErrors.length == 0) {
    
        /* close the popup */
        closeChangeCreditCard();
        
        /* display success message*/
        displayPopup("Your credit card has been changed successfully. Please update other fields if necessary", "center", 3000);

        /* update locked fields */
        populate(xhresponse, "CreditCardType", "selCreditCardType");
        populatePartialText(xhresponse, "CreditCardNumber", "txtCreditCardNumber");

        return;
    }
    /* handle the errors */
    var nodError;
    var iErrorCode;
    var strErrorMessage;

    for (var i = 0; i < nodErrors.length; i++) {
        nodError = nodErrors[i];

        iErrorCode = parseInt(nodError.getElementsByTagName("ErrorCode")[0].firstChild.nodeValue);
        strErrorMessage = nodError.getElementsByTagName("ErrorMessage")[0].firstChild.nodeValue;

        if (iErrorCode == SYS_ERR)
            showDiv(document.getElementById("divCreditCardSystemError"), strError);
        else
            setError(iErrorCode, strErrorMessage)
    }
    /* display friendly error message */
    displayPopup("There are invalid fields. Kindly resolve the marked areas", "center", 2500);
}
/*****************************************************************************/
/* Show change credit card popup*/
function showChangeCreditCard() {
    
    showPopupFieldset(document.getElementById("fsetCreditCardNumber"));

    /* clears textboxes and errors */
    hideDiv(document.getElementById("divNewCreditCardNumberError"));
    hideDiv(document.getElementById("divNewCreditCardTypeError"));

    document.getElementById("txtNewCreditCardNumber").value = "";
    document.getElementById("selNewCreditCardType").value = -1;
}
/*****************************************************************************/
function closeChangeCreditCard() {
    hidePopFieldset(document.getElementById("fsetCreditCardNumber"));
}
/*****************************************************************************/

/* Credit card details fieldset*/
function showCreditCardFieldset() {
    document.getElementById("fsetCreditCardDetails").style.visibility = "visible";
}
/*****************************************************************************/
function hideCreditCardFieldset() {
    document.getElementById("fsetCreditCardDetails").style.visibility = "hidden";
}
/*****************************************************************************/

/* Helpers */
function showPopupFieldset(fset) {

    fset.style.display = "block";
    setControlsEnabled(document.getElementById("fsetRegistrationDetails"), false);
    setControlsEnabled(document.getElementById("fsetCreditCardDetails"), false);

    /* TODO: fix this bug, need to enable back pop up controls */
    var ip = fset.getElementsByTagName("input");
    for (var i = 0; i < ip.length; i++)
        ip[i].disabled = false;
        
    ip = fset.getElementsByTagName("select");
    for (i = 0; i < ip.length; i++)
        ip[i].disabled = false;
}
/*****************************************************************************/
function hidePopFieldset(fset) {

    fset.style.display = "none";

    /* recover these fieldsets */
    setControlsEnabled(document.getElementById("fsetRegistrationDetails"), true);
    setControlsEnabled(document.getElementById("fsetCreditCardDetails"), true);
}
/*****************************************************************************/

/* if disable, grays out the fieldset */
function setControlsEnabled(fset, enable) {

    /* setup some paramters based on enable*/
    var strBackgroundColor;
    var strBackgroundImage;

    if (enable) {
        strBackgroundColor = "#FFFFFF";
        strBackgroundImage = "url(images/background.png)";
        strOpacityIE = "none";
    }
    else {
        strBackgroundColor = "#C0C0C0";     // greyed out fieldset
        strBackgroundImage = "none";        // so that the color can take effect
    }

    /* fieldset background */
    fset.style.backgroundColor = strBackgroundColor;
    fset.style.backgroundImage = strBackgroundImage;

    /* Save Credit Card division */
    document.getElementById("divSaveCreditCard").style.backgroundColor = strBackgroundColor;

    /* inputs */
    var ctrls = fset.getElementsByTagName("input");
    for (i = 0; i < ctrls.length; i++)
        ctrls[i].disabled = !enable;

    /* selects */
    ctrls = fset.getElementsByTagName("select");
    for (i = 0; i < ctrls.length; i++)
        ctrls[i].disabled = !enable;

    /* Save and Reset buttons */
    document.getElementById("btnSave").disabled = !enable;
    document.getElementById("btnReset").disabled = !enable;

    /* always disable */
    document.getElementById("txtEmail").disabled = true;
    document.getElementById("txtPassword").disabled = true;
    document.getElementById("txtCustomerID").disabled = true;
    document.getElementById("txtDOB").disabled = true;
    document.getElementById("txtRegisteredOn").disabled = true;
    document.getElementById("txtCreditCardNumber").disabled = true;
    document.getElementById("selCreditCardType").disabled = true;
}
/*****************************************************************************/
function populate(xml_response, tag_name, element_id) {

    var nod = xml_response.getElementsByTagName(tag_name);
    if (nod.length > 0) {
        if(nod[0].hasChildNodes()){
            var strTagValue = nod[0].firstChild.nodeValue;
            document.getElementById(element_id).value = strTagValue
        }
    }
}
/*****************************************************************************/
/* mask tailing 5th to last character, for textbox only */
function populatePartialText(xml_response, tag_name, element_id) {

    var nod = xml_response.getElementsByTagName(tag_name);
    if (nod.length > 0) {
        if (nod[0].hasChildNodes()) {
            
            var strTagValue = nod[0].firstChild.nodeValue;

            if (strTagValue.length > 4)
                document.getElementById(element_id).value = strTagValue.substring(0, 4) + " xxx..."
            else
                document.getElementById(element_id).value = "xxx...";
        }
    }
}
/*****************************************************************************/
function setError(code, msg) {

    switch (code) {
        case SYS_ERR:
            showDiv(document.getElementById("divSystemError"), msg);
            break;

        case EMAIL_NT_UNIQUE: case INVALID_EMAIL: case NULL_EMAIL:
            showDiv(document.getElementById("divEmailError"), msg);
            break;
            
        case INVALID_CONTACT:
            showDiv(document.getElementById("divContactError"), msg);
            break;
            
        case INVALID_CC_EXPIRY: case CC_EXPIRED:
            showDiv(document.getElementById("divCreditCardExpiryError"), msg);
            break;
            
        case INVALID_CC_NUM:
            showDiv(document.getElementById("divNewCreditCardNumberError"), msg);
            break;
            
        case INVALID_CC_ISSUER:
            showDiv(document.getElementById("divNewCreditCardTypeError"), msg);
            break;
            
        case NULL_FST_NAME:
            showDiv(document.getElementById("divFirstNameError"), msg);
            break;
            
        case NULL_LST_NAME:
            showDiv(document.getElementById("divLastNameError"), msg);
            break;
            
        case PWD_MISMATCH:
            showDiv(document.getElementById("divRetypePasswordError"), msg);
            break;

        case NULL_PWD: case INVALID_PWD:
            showDiv(document.getElementById("divNewPasswordError"), msg);
            break;
    }
}
/*****************************************************************************/
function clearAllErrors() {

    hideDiv(document.getElementById("divFirstNameError"));
    hideDiv(document.getElementById("divLastNameError"));
    hideDiv(document.getElementById("divContactError"));
    hideDiv(document.getElementById("divCreditCardExpiryError"));
    hideDiv(document.getElementById("divEmailError"));
    hideDiv(document.getElementById("divSystemError"));
}
/*****************************************************************************/
function clearError(div) {
    hideDiv(div);
}